package com.yanqu.road.server.manger.sdk;

import com.yanqu.road.entity.push.ClientPushRecord;
import com.yanqu.road.entity.servercenter.ClientPushContent;
import com.yanqu.road.logic.bussiness.player.UserPushMsgBusiness;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.thread.client.msg.push.task.PushClientMsgTask;
import com.yanqu.road.server.timer.client.msg.ClientPushMsgTask;
import com.yanqu.road.utils.LocalDateTimeHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2021/3/21
 */
public class ClientMsgPushMgr extends TempMgr {

    private static ThreadTaskManger clientMgrPushThreads;
    static Map<Long, Map<Integer, ClientPushRecord>> clientPushRecordMap;
    static Map<Integer, ClientPushContent> clientPushContentMap;

    public static Map<Long, Map<Integer, ClientPushRecord>> getClientPushRecordMap() {
        return clientPushRecordMap;
    }

    @Override
    public boolean reloadData() throws Exception {
        if (clientMgrPushThreads == null) {
            clientMgrPushThreads = new ThreadTaskManger(1, "Client_Msg_Push_Thread_Pool");
        }
        clientPushRecordMap = UserPushMsgBusiness.getClientPushRecordMap();
        clientPushContentMap = UserPushMsgBusiness.getClientPushContentMap();
        return true;
    }

    public static ClientPushContent getClientPushContent(int type) {
        return clientPushContentMap.get(type);
    }

    public static Map<Integer, ClientPushContent> getClientPushContentMap() {
        return clientPushContentMap;
    }

    public static void addClientMsgPushTask(PushClientMsgTask pushClientMsgTask) {
        clientMgrPushThreads.addTask(1, pushClientMsgTask);
    }

    public static ClientPushRecord getClientPushRecord(long userId, int type) {
        if (clientPushRecordMap.get(userId) == null) {
            clientPushRecordMap.put(userId, new ConcurrentHashMap<>());
        }

        if (clientPushRecordMap.get(userId).get(type) == null) {
            ClientPushRecord clientPushRecord = new ClientPushRecord();
            clientPushRecord.setUserId(userId);
            clientPushRecord.setType(type);
            clientPushRecord.setTimes(0);
            clientPushRecord.setUpdateTime(System.currentTimeMillis());
            clientPushRecord.setInsertOption();
            clientPushRecordMap.get(userId).put(type, clientPushRecord);
        }
        ClientPushRecord clientPushRecord = clientPushRecordMap.get(userId).get(type);
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        if (clientPushRecord.getUpdateTime() < zeroTime) {
            clientPushRecord.setTimes(0);
            clientPushRecord.setUpdateTime(System.currentTimeMillis());
        }

        return clientPushRecord;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (Map<Integer, ClientPushRecord> clientPushRecordValues : clientPushRecordMap.values()) {
            for (ClientPushRecord clientPushRecord : clientPushRecordValues.values()) {
                if (clientPushRecord.isInsertOption()) {
                    UserPushMsgBusiness.addClientPushRecord(clientPushRecord);
                } else if (clientPushRecord.isUpdateOption()) {
                    UserPushMsgBusiness.updateClientPushRecord(clientPushRecord);
                }
            }
        }
        return true;
    }
}
